import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class TestFunctions {


	public static void main(String[] args) {

		MultiThreadedHttpConnectionManager connectionMgr = new MultiThreadedHttpConnectionManager();
		connectionMgr.getParams().setMaxConnectionsPerHost(new HostConfiguration(),Integer.valueOf("5"));
		connectionMgr.getParams().setMaxTotalConnections(Integer.valueOf("10"));		
		HttpClient httpClient = new HttpClient(connectionMgr);		
		
		
		FileInputStream inputStream=null;
		try {
			inputStream = new FileInputStream ("E:/temp/input.json");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//byte[] bytes = "Test".getBytes();
	    
		InputStream is = new BufferedInputStream(inputStream);
		
		//post(httpClient, "http://localhost:8080/notifications/api/sweep?days=5", is, null, null);
	     post(httpClient, "http://localhost:8080/notifications/api/feed", is, null, null);		
	}

	public static String post (HttpClient httpClient, String url, InputStream body, Map<String, String> headers, Map<String, String> params) {

		url = makeUrl (url, params);

		final PostMethod filePost = new PostMethod( url);

		try
		{
			filePost.setRequestBody(body);
			
			if (null != headers) {
				Iterator it = headers.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry pairs = (Map.Entry)it.next();
					filePost.addRequestHeader(pairs.getKey().toString(), pairs.getValue().toString());
				}
			}

			try {
				int status = httpClient.executeMethod( filePost );
				
				if (status != 200) {
					System.out.print("Failure" + status);
				}
				return filePost.getResponseBodyAsString();
			} catch (Exception e) {
				//log.error("Exception Ocurred ",e);
			}
		}finally {
			filePost.releaseConnection();
		}
		return "";
	}
	
	public String get(HttpClient httpClient, String url) {
		URI uri = null;
		try {
			uri = new URI(url, false);
		} catch (Exception e2) {
			// log.info("URI Exception", e2);
		}

		GetMethod get = new GetMethod(url);
		// log.info("URL submitted on Get:" + url);

		get.setFollowRedirects(false);

		String responseStr = "";
		try {
			int iGetResultCode = httpClient.executeMethod(get);

			if (200 == iGetResultCode) {
				responseStr = get.getResponseBodyAsString();
			} else {
				 //log.info("Couldnot connect , error is " + iGetResultCode);
			}
		} catch (Exception e) {
			// log.error("Issues with Request " + e);
		} finally {
			get.releaseConnection();
		}

		return responseStr;
	}	
	
	private static String makeUrl(String url, Map<String, String> parameters) {
		boolean and = url.indexOf("?") != -1;
		if (parameters != null) {
			for (Map.Entry<String, String> param : parameters.entrySet()) {
				try {
					url += (and ? "&" : "?") + param.getKey() + "=" + URLEncoder.encode(param.getValue(),"UTF-8");
					and = true;
				} catch (Exception e) {
					//log.error("Exception Ocurred ",e);
				}
			}
		}
		return url;
	}		
	
	private void testCreateUser(HttpClient httpClient) {
		String url = "http://localhost:8080/soshal/api/1.0/user/";
		String inputStream = "{\"acessToken\":\"sess1\",\"userName\":\"rajesh_vashista\",\"firstName\":\"Rajesh\",\"lastName\":\"Vashista\",\"city\":\"Delhi\",\"country\":\"India\"}";

		InputStream is = new ByteArrayInputStream(inputStream.getBytes());
		
		String response = post(httpClient, url, is, null, null);
		
		// Map values = getValues(response);
		System.out.println(response);
	}
	
//	private void testUpdateUser() {
//		String url = "http://localhost:8080/soshal/api/1.0/user/";
//		String inputStream = "{\"acessToken\":\"sess1\",\"userName\":\"rajesh_vashista\",\"firstName\":\"Rajesh\",\"lastName\":\"Vashista\",\"city\":\"Delhi\",\"country\":\"India\"}";
//
//		InputStream is = new ByteArrayInputStream(inputStream.getBytes());
//		
//		String response = post(httpClient, url, is, null, null);
//		System.out.println(response);		
//	}	
//	
//	private Map getValues(String response) {
//		String sessionId = response.substring(response.indexOf("sessionId"));
//		String sessionValue = sessionId.substring(12, 32);
//	}
//	
//	private void testGetUser() {
//		
//	}	
}
